package com.project.controller.web;

import com.project.entity.Class;
import com.project.entity.StudentInfo;
import com.project.entity.User;
import com.project.myBeans.R;
import com.project.myBeans.StudentInfoDetail;
import com.project.service.ClassService;
import com.project.service.StudentInfoService;
import com.project.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.web.bind.annotation.*;

@EnableAutoConfiguration
@RestController
@Api(description = "学生信息接口",value = "studentInfo")
@RequestMapping("/studentinfo")
public class StudentInfoController {

    @Autowired
    UserService userService;

    @Autowired
    StudentInfoService studentInfoService;

    @Autowired
    ClassService classService;

    /**
     * wwh 2018/3/25
     * @param userid
     * @return
     */
    @ApiOperation("根据userid查找studentInfo")
    @GetMapping("/findByUserid/{userid}")
    public R findByUserId(@PathVariable Integer userid){
        try{
            return R.ok().put("data",studentInfoService.findByUserId(userid));
        }catch (Exception e){
            e.printStackTrace();
            return R.error();
        }
    }

    @ApiOperation("通过studentInfoId查找学生详情")
    @GetMapping("/findDetailById/{id}")
    public R findDetailById(@PathVariable Integer id){
        try {
            StudentInfoDetail studentInfoDetail = new StudentInfoDetail();
            StudentInfo studentInfo = studentInfoService.findById(id);
            studentInfoDetail.setStudentInfo(studentInfo);
            if(studentInfo.getUserid()!=null) {
                studentInfoDetail.setUser(userService.findById(studentInfo.getUserid()));
            }
            if(studentInfo.getClassid()!=null) {
                studentInfoDetail.setClazz(classService.findById(studentInfo.getClassid()));
            }
            return R.ok().put("data",studentInfoDetail);
        }catch (Exception e){
            e.printStackTrace();
            return R.error();
        }
    }

    @ApiOperation("单个添加学生信息")
    @PostMapping("/addDetailOne")
    public R addDetailOne(@RequestBody StudentInfoDetail studentInfoDetail){
        try{
            if((studentInfoDetail.getStudentInfo()==null||studentInfoDetail.getStudentInfo().getClassid() == null) && studentInfoDetail.getClazz().getId() == null){
                return R.error("请选择班级");
            }else {
                Class clazz = null;
                if(studentInfoDetail.getStudentInfo().getClassid() != null){
                    clazz = classService.findById(studentInfoDetail.getStudentInfo().getClassid());
                }else if(studentInfoDetail.getClazz().getId() != null){
                    clazz = classService.findById(studentInfoDetail.getClazz().getId());
                }

                if(clazz == null){
                    return R.error("班级不存在");
                }

                User user = studentInfoDetail.getUser();
                if(userService.findByUserName(user.getUsername()) != null){
                    return R.error("该用户已存在");
                }else {
                    user.setUniversityid(clazz.getUniversityid());
                    user.setSchoolid(clazz.getSchoolid());
                    userService.add(user);

                    StudentInfo studentInfo = studentInfoDetail.getStudentInfo();
                    studentInfo.setClassid(clazz.getId());
                    studentInfo.setMajorid(clazz.getMajorid());
                    studentInfo.setDepartmentid(clazz.getDepartmentid());
                    studentInfo.setSchoolid(clazz.getSchoolid());
                    studentInfo.setUserid(user.getId());
                    studentInfoService.add(studentInfo);
                    return R.ok();
                }

            }
        }catch (Exception e){
            e.printStackTrace();
            return R.error();
        }
    }


}
